<?php

namespace app\api\model;

use think\Model;

class UserVip extends Model
{
    protected $hidden = ['createtime', 'updatetime', 'online', 'pivot'];

    public function getImageAttr($value)
    {
        return $value ? request()->domain().$value : '';
    }

    public static function getByDirectUpgrade($direct_upgrade)
    {
        return self::where('direct_upgrade', '<=', $direct_upgrade)->order('direct_upgrade desc')->find();
    }

    public static function getByAmassUpgrade($amass_upgrade)
    {
        return self::where('amass_upgrade', '<=', $amass_upgrade)->order('amass_upgrade desc')->find();
    }

    public static function getVips()
    {
        return self::with('equity')->where('online', 1)->order('name asc')->select();
    }

    public function equity()
    {
        return $this->belongsToMany(UserEquity::class, 'user_vip_equity', 'equity_id', 'user_vip_id');
    }

    public static function getDiscountByName($name)
    {
        return self::where('name', $name)->value('discount');
    }

    /**
     * 最优惠折扣
     */
    public static function getLastDiscount()
    {
        return self::where('online', 1)->min('discount');
    }

    public static function getVipIds()
    {
        return self::where('online', 1)->column('id');
    }

    public static function getIdByName($name)
    {
        return self::where('name', $name)->value('id');
    }
}
